//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.cgreport.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;
import com.ht.query.QueryGenerator;
import com.ht.utils.DateUtils;
import com.ht.utils.oConvertUtils;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CgReportQueryParamUtil {
    private static final Logger log = LoggerFactory.getLogger(CgReportQueryParamUtil.class);

    public CgReportQueryParamUtil() {
    }

    public static void loadQueryParams(HttpServletRequest request, Map<String, Object> item, Map<String, Object> pageSearchFields, Map<String, Object> paramData) {
        String filedName = (String)item.get("field_name");
        String queryMode = (String)item.get("search_mode");
        String filedType = (String)item.get("field_type");
        String value;
        String end;
        String contiansChinesevalue;
        if ("single".equals(queryMode)) {
            value = request.getParameter(filedName.toLowerCase());

            try {
                if (oConvertUtils.isEmpty(value)) {
                    return;
                }

                end = request.getQueryString();
                if (end.contains(filedName + "=")) {
                    contiansChinesevalue = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                    value = contiansChinesevalue;
                }
            } catch (UnsupportedEncodingException var10) {
                log.error(var10.getMessage(), var10);
                return;
            }

            if (oConvertUtils.isNotEmpty(value)) {
                if (value.contains("*")) {
                    value = value.replaceAll("\\*", "%");
                    pageSearchFields.put(filedName, " LIKE :" + filedName);
                } else {
                    pageSearchFields.put(filedName, " = :" + filedName);
                }
            }

            paramData.put(filedName, covertData(filedType, value, true));
        } else if ("group".equals(queryMode)) {
            value = request.getParameter(filedName.toLowerCase() + "_begin");
            end = request.getParameter(filedName.toLowerCase() + "_end");
            if (oConvertUtils.isNotEmpty(value)) {
                contiansChinesevalue = " >= :" + filedName + "_begin";
                pageSearchFields.put(filedName, contiansChinesevalue);
                paramData.put(filedName + "_begin", covertData(filedType, value, true));
            }

            if (oConvertUtils.isNotEmpty(end)) {
                contiansChinesevalue = " <= :" + filedName + "_end";
                pageSearchFields.put(new String(filedName), contiansChinesevalue);
                paramData.put(filedName + "_end", covertData(filedType, end, false));
            }
        }

    }

    private static Object covertData(String fieldType, String value, boolean isBegin) {
        Object obj = null;
        if (oConvertUtils.isNotEmpty(value)) {
            if ("String".equalsIgnoreCase(fieldType)) {
                obj = value;
            } else if ("Date".equalsIgnoreCase(fieldType)) {
                if (value.length() != 19 && value.length() == 10) {
                    if (isBegin) {
                        value = value + " 00:00:00";
                    } else {
                        value = value + " 23:59:59";
                    }
                }

                SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                obj = DateUtils.str2Date(value, datetimeFormat);
            } else if ("Double".equalsIgnoreCase(fieldType)) {
                obj = value;
            } else if ("Integer".equalsIgnoreCase(fieldType)) {
                obj = value;
            } else {
                obj = value;
            }
        }

        return obj;
    }

    public static String getJson(List<Map<String, Object>> result, Long size) {
        JSONObject main = new JSONObject();
        JSONArray rows = new JSONArray();
        main.put("total", size);
        if (result != null) {
            Iterator var4 = result.iterator();

            while(var4.hasNext()) {
                Map m = (Map)var4.next();
                JSONObject item = new JSONObject();

                String key;
                String value;
                for(Iterator it = m.keySet().iterator(); it.hasNext(); item.put(key, value)) {
                    key = (String)it.next();
                    value = String.valueOf(m.get(key));
                    key = key.toLowerCase();
                    if (key.contains("time") || key.contains("date")) {
                        value = datatimeFormat(value);
                    }
                }

                rows.add(item);
            }
        }

        main.put("rows", rows);
        return main.toString();
    }

    public static String getJson(List<Map<String, Object>> result) {
        JSONArray rows = new JSONArray();
        Iterator var2 = result.iterator();

        while(var2.hasNext()) {
            Map m = (Map)var2.next();
            JSONObject item = new JSONObject();

            String key;
            String value;
            for(Iterator it = m.keySet().iterator(); it.hasNext(); item.put(key, value)) {
                key = (String)it.next();
                value = String.valueOf(m.get(key));
                key = key.toLowerCase();
                if (key.contains("time") || key.contains("date")) {
                    value = datatimeFormat(value);
                }
            }

            rows.add(item);
        }

        return rows.toString();
    }

    public static String datatimeFormat(String datetime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        SimpleDateFormat dateFormatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;

        try {
            d = dateFormat.parse(datetime);
            return dateFormatTo.format(d);
        } catch (Exception var5) {
            return datetime;
        }
    }

    public static String getListQueryConditionSql(List<OnlCgreportItem> fieldList, Map<String, Object> params, String alias) {
        StringBuffer sb = new StringBuffer();
        Iterator var4 = fieldList.iterator();

        while(true) {
            String field;
            String dbtype;
            Object value_begin;
            do {
                while(true) {
                    OnlCgreportItem item;
                    do {
                        if (!var4.hasNext()) {
                            return sb.toString();
                        }

                        item = (OnlCgreportItem)var4.next();
                        field = item.getFieldName();
                        dbtype = item.getFieldType();
                    } while(1 != item.getIsSearch());

                    if ("single".equals(item.getSearchMode())) {
                        value_begin = params.get(field);
                        break;
                    }

                    value_begin = params.get(field + "_begin");
                    if (value_begin != null) {
                        sb.append(" and " + alias + field + " >= ");
                        if (!"Long".equals(dbtype) && !"Integer".equals(dbtype)) {
                            sb.append("'" + value_begin.toString() + "'");
                        } else {
                            sb.append(value_begin.toString());
                        }
                    }

                    Object value_end = params.get(field + "_end");
                    if (value_end != null) {
                        sb.append(" and " + alias + field + " <= ");
                        if (!"Long".equals(dbtype) && !"Integer".equals(dbtype)) {
                            sb.append("'" + value_end.toString() + "'");
                        } else {
                            sb.append(value_end.toString());
                        }
                    }
                }
            } while(value_begin == null);

            String temp = QueryGenerator.getSingleQueryConditionSql(field, alias, value_begin, !"Long".equals(dbtype) && !"Integer".equals(dbtype));
            sb.append(" and " + temp);
        }
    }
}
